/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "list数值插座.h"
#include "Context/当前默认操作数据.h"

#include "list元素插座.h"
#include "list矢量插座.h"
#include "list物体插座.h"
#include "list物理物体插座.h"
#include "../通用插座.h"
#include "../../节点树.h"

#include <系统/多线程.h>
#include <thread>
#include <ppl.h>





//using namespace std;



C_插座Str_Array1D::C_插座Str_Array1D(std::wstring name) :C_插座基类(name, E_值类型::e_Type_Str_1D) {

}

void* C_插座Str_Array1D::f_getData(uint8 线程ID) {
	return nullptr;
}

bool C_插座Str_Array1D::f_getBool(uint8 线程ID) {
	return false;
}

void C_插座Str_Array1D::f_setData(void* data, uint8 线程ID) {
}












C_插座I8_Array1D::C_插座I8_Array1D(std::wstring name) :C_插座基类(name, E_值类型::e_Type_I8_1D) {
	m_默认 = (S_I8Array*)f_core_array_new(1, sizeof(int8));
	m_转换 = (S_I8Array*)f_core_array_new(1, sizeof(int8));

	m_Value = m_默认;

	m_默认->ptr_userData[0] = 0;
	m_转换->ptr_userData[0] = 0;
}

C_插座I8_Array1D::~C_插座I8_Array1D() {
	f_core_array_free((S_Array*)m_默认);
	f_core_array_free((S_Array*)m_转换);

}

void * C_插座I8_Array1D::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		switch (socket->m_Type) {
			case E_值类型::e_Type_Array:
			case E_值类型::e_Type_I8_1D: {
				return socket->f_getData(0);
			}
			case E_值类型::e_Type_I32_1D: {
				const auto& data = *DEF_I32插座_1D数据(socket, 0);
				//m_Value.assign(data.begin(), data.end());
				break;
			}

			
		}
		return m_转换;
	}
	else if (!m_映射指针) {
		m_Value = m_默认;
	}
	return m_Value;
}

void C_插座I8_Array1D::f_setData(void* data, uint8 线程ID) {
	if (data) {
		m_Value = ((S_I8Array*)data);
		m_映射指针 = true;
	}
	else {
		m_Value = m_默认;
		m_映射指针 = false;
	}
}

std::string C_插座I8_Array1D::f_构建变量代码(int32 loc) {
	std::string code;
	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == e_Type_I8_1D || socket->m_Type == e_Type_Array) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + f_整数值转字符串(loc) + "new_val_to_I8s";

		code += DEF_S(S_I8Array*)" " + m_Code + " = ";
		switch (socket->m_Type) {
			case e_Type_I8:
			case e_Type_I32:
			case e_Type_UI32:
			case e_Type_F32: {
				code += DEF_S(f_f32_array_fromF32_alloc)"(" + socket->f_getCode(loc) + ");\n";
				break;
			}
			case e_Type_I32_1D: code += DEF_S(f_array_I8fromI32_alloc)"(" + socket->f_getCode(loc) + ");\n"; break;
			case e_Type_UI32_1D: code += DEF_S(f_array_I8fromUI32_alloc)"(" + socket->f_getCode(loc) + ");\n"; break;
			case e_Type_F32_1D: code += DEF_S(f_array_I8fromF32_alloc)"(" + socket->f_getCode(loc) + ");\n"; break;
			
			default:
				break;
		}

		m_pNode->m_ReleaseCode += f_代码构建_销毁数组(m_Code);
	}
	else {
		C_插座基类::f_构建变量代码(loc);
		m_Code = "(&" + m_Code + ")";
	}

	return code;
}

bool C_插座I8_Array1D::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_Array:
		case E_值类型::e_Type_I8:
		case E_值类型::e_Type_I32:
		case E_值类型::e_Type_UI32:
		case E_值类型::e_Type_F32:
		case E_值类型::e_Type_I8_1D:
		case E_值类型::e_Type_I32_1D:
		case E_值类型::e_Type_UI32_1D:
		case E_值类型::e_Type_F32_1D:
			return true;
	}
	return false;
}





C_插座Bool_1D::C_插座Bool_1D(std::wstring name) : C_插座基类(name, E_值类型::e_Type_Bool_1D) {
	m_默认 = (S_BoolArray*)f_core_array_new(1, sizeof(bool));
	m_转换 = (S_BoolArray*)f_core_array_new(1, sizeof(bool));
	m_Value = m_默认;

	m_默认->ptr_userData[0] = 0;
	m_转换->ptr_userData[0] = 0;
}

void* C_插座Bool_1D::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(0);
	if (socket) {
		switch (socket->m_Type) {
		/*case E_值类型::e_Type_F32_1D:
		case E_值类型::e_Type_Vec4_1D:
		case E_值类型::e_Type_Vec3_1D:
		case E_值类型::e_Type_多边形边1D:
		case E_值类型::e_Type_Array:*/
		case E_值类型::e_Type_Bool_1D:
			return socket->f_getData(0);
		}
	}
	return m_Value;
}

void C_插座Bool_1D::f_setData(void* data, uint8 线程ID) {
	m_Value;
}














C_Vector1D_F32插座::C_Vector1D_F32插座(std::wstring name)
	:C_插座基类(name, E_值类型::e_Type_F32_Array1D)
{
	m_Value.resize(1);
}

C_Vector1D_F32插座::~C_Vector1D_F32插座()
{
}


void* C_Vector1D_F32插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_F32_Array1D) {
			return dynamic_cast<C_Vector1D_F32插座*>(socket)->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_I32_1D) {
			const auto& data = *DEF_I32插座_1D数据(socket, 线程ID);
			//uint32 num = uint32(data.size());
			//m_Value.resize(num);
			//for (uint32 i = 0; i < num; ++i) m_Value[i] = data[i];
		}
		else if (socket->m_Type == E_值类型::e_Type_UI32_1D) {
			//const auto& data = *DEF_UI32插座_1D数据(socket, 线程ID);
			//m_Value.assign(data.begin(), data.end());
		}
		else if (socket->m_Type == E_值类型::e_Type_I8_1D) {
			const auto& data = *DEF_I8插座_1D数据(socket, 0);
			//m_Value.assign(data.begin(), data.end());
		}
		else if (socket->m_Type == E_值类型::e_Type_F32) {
			auto data = DEF_F32插座数据(socket, 0);
			m_Value.resize(1);
			m_Value[0] = data;
		}
		else if (socket->m_Type == E_值类型::e_Type_I32) {
			auto data = DEF_I32插座数据(socket, 0);
			m_Value.resize(1);
			m_Value[0] = data;
		}
		else if (socket->m_Type == E_值类型::e_插座Type_引用数据) {
			auto data = DEF_引用插座数据(socket, 0);
			//m_Value.clear();

			switch (data->m_Type) {
			case E_值类型::e_Type_F32:
				m_Value.resize(1);
				m_Value[0] = *((float32*)data->m_Data);
				break;
			case E_值类型::e_Type_I32:
				m_Value.resize(1);
				m_Value[0] = *((int32*)data->m_Data);
				break;
			case E_值类型::e_Type_F32_Array1D: 
			{
				auto a = *((std::vector<float32>*)data->m_Data);
				if (data->m_添加方式 == E_数据赋值方式::e_赋值_添加) {
					m_Value.insert(m_Value.end(), a.begin(), a.end());
				}
				else {
					m_Value.assign(a.begin(), a.end());
				}
				
			}
				break;
			
			default:
				break;
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_Array元素) {
		}
	}
	return &m_Value;
}


void C_Vector1D_F32插座::f_setData(void* data, uint8 线程ID) {
	S_引用插座数据映射* mapData = (S_引用插座数据映射*)(data);
	switch (mapData->m_Type)
	{
	case E_值类型::e_Type_F32_Array1D:
		if (mapData->m_添加方式 == E_数据赋值方式::e_赋值_添加) {

		}
		m_Value = *((std::vector<float32>*)(mapData->m_Data));
		return;
	case E_值类型::e_Type_F32:
		if (mapData->m_添加方式 == E_数据赋值方式::e_赋值_添加) {
			m_Value.push_back(*((float32*)(mapData->m_Data)));
		}
		else {
			m_Value.resize(1);
			m_Value[0] = *((float32*)(mapData->m_Data));
		}
		return;
	case E_值类型::e_Type_I32:
		if (mapData->m_添加方式 == E_数据赋值方式::e_赋值_添加) {
			m_Value.push_back(*((int32*)(mapData->m_Data)));
		}
		else {
			m_Value.resize(1);
			m_Value[0] = *((int32*)(mapData->m_Data));
		}
		return;
	default:
		break;
	}

	m_Value = *((std::vector<float32>*)data);
}

void C_Vector1D_F32插座::f_释放数据() {
	m_Value.clear();
}








C_F32_1D插座::C_F32_1D插座(std::wstring name) :C_插座基类(name, E_值类型::e_Type_F32_1D) {
	m_默认值 = (S_F32Array*)f_core_array_new(1, sizeof(float32));
	m_转换值 = (S_F32Array*)f_core_array_new(1, sizeof(float32));
	m_Value = m_默认值;
	m_默认值->ptr_userData[0] = 0;
	m_转换值->ptr_userData[0] = 0;
}

C_F32_1D插座::~C_F32_1D插座() {
	f_core_array_free((S_Array*)m_默认值);
	f_core_array_free((S_Array*)m_转换值);
}

S_GPU内存块索引& C_F32_1D插座::f_getGPU块(uint8 线程ID, bool 更新) {
	C_插座基类::f_getGPU块(线程ID, 更新);
	if (!m_GPU缓存.m_Buf) {
		m_GPU缓存 = f_buf_alloc(f_NodeCtx_get默认场景()->m_光追后端->m_F32动态属性, 1);
	}

	if (更新) {
		S_F32Array* data = (S_F32Array*)f_getData(线程ID);
		f_gbuf_fill(m_GPU缓存, data->ptr_userData, 0, data->count);
	}
	return m_GPU缓存;
}

S_GPU内存块索引& C_F32_1D插座::f_downGPU块(uint8 线程ID, bool 更新) {
	S_F32Array* data = (S_F32Array*)f_getData(线程ID);
	if (m_GPU缓存.m_Buf) {
		f_buf_down(m_GPU缓存, (S_Array*)data);
	}
	return m_GPU缓存;
}

void* C_F32_1D插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		switch (socket->m_Type){
			case E_值类型::e_Type_Array:
			case E_值类型::e_Type_F32_1D : {
				return socket->f_getData(0);
			} 
			case E_值类型::e_Type_I32_1D: {
				auto* data = DEF_I32插座_1D数据(socket);
				f_core_array_resize((S_Array*)m_转换值, data->count);
				for (uint32 i = 0; i < data->count; ++i) {
					m_转换值->ptr_userData[i] = data->ptr_userData[i];
				}
				break;
			}
			case  E_值类型::e_Type_I8_1D: {
				auto* data = DEF_I8插座_1D数据(socket);
				f_core_array_resize((S_Array*)m_转换值, data->count);
				for (uint32 i = 0; i < data->count; ++i) {
					m_转换值->ptr_userData[i] = data->ptr_userData[i];
				}
				break;
			}
			case e_Type_I32: {
				auto& data = DEF_I32插座数据(socket);
				f_core_array_resize((S_Array*)m_转换值, 1);
				m_转换值->ptr_userData[0] = data;
				break;
			}
			case e_Type_UI32: {
				auto& data = DEF_UI32插座数据(socket);
				f_core_array_resize((S_Array*)m_转换值, 1);
				m_转换值->ptr_userData[0] = data;
				break;
			}
			case e_Type_F32: {
				auto& data = DEF_F32插座数据(socket);
				f_core_array_resize((S_Array*)m_转换值, 1);
				m_转换值->ptr_userData[0] = data;
				break;
			}
		}
		return m_转换值;
	}
	else if (!m_映射指针) {
		m_Value = m_默认值;
	}
	return m_Value;
}

void C_F32_1D插座::f_setData(void* data, uint8 线程ID) {
	if (data) {
		m_Value = (S_F32Array*)data;
		m_映射指针 = true;
	}
	else {
		m_Value = m_默认值;
		m_映射指针 = false;
	}
	
}

std::string C_F32_1D插座::f_构建变量代码(int32 loc) {
	std::string code;
	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == e_Type_F32_1D || socket->m_Type == e_Type_Array) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + f_整数值转字符串(loc) + "new_val_to_F32s";

		code += DEF_S(S_F32Array*)" " + m_Code + " = ";
		switch (socket->m_Type) {
		case e_Type_I32:
		case e_Type_UI32:
		case e_Type_F32: {
			code += DEF_S(f_f32_array_fromF32_alloc)"(" + socket->f_getCode(loc) + ");\n";
			break;
		}
		case E_值类型::e_Type_I32_1D: {
			code += DEF_S(f_f32_array_fromI32_alloc)"(" + socket->f_getCode(loc) + ");\n";
			break;
		}
		case E_值类型::e_Type_I8_1D: {
			code += DEF_S(f_array_F32fromI8_alloc)"(" + socket->f_getCode(loc) + ");\n";
			break;
		}
		case E_值类型::e_Type_UI32_1D: {
			code += DEF_S(f_array_F32fromUI32_alloc)"(" + socket->f_getCode(loc) + ");\n";
			break;
		}
		default:
			std::cout<<"error: " << m_pNode->f_getName() << f_getName() << "插座接入没有实现转换" << std::endl;
			break;
		}

		m_pNode->m_ReleaseCode += f_代码构建_销毁数组(m_Code);
	}
	else {
		C_插座基类::f_构建变量代码(loc);
		m_Code = "(&" + m_Code + ")";
	}
	
	return code;
}

bool C_F32_1D插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_I32:
		case E_值类型::e_Type_UI32:
		case E_值类型::e_Type_F32:
		case E_值类型::e_Type_I8_1D:
		case E_值类型::e_Type_I32_1D:
		case E_值类型::e_Type_UI32_1D:
		case E_值类型::e_Type_F32_1D:
			return true;
	}
	return false;
}

void C_F32_1D插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
	fread(m_默认值->ptr_userData, sizeof(float32), 1, f);
}

void C_F32_1D插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);
	fwrite(m_默认值->ptr_userData, sizeof(float32), 1, f);
}











template<typename T1, typename T2>
void f_Array1D数据转Array2D数据(T1& 目标, T2& 源) {
	uint32 num = 源.size();
	目标.resize(num);

	auto* p1 = 源.data();
	auto* p2 = 目标.data();
//#pragma loop(on_vector)
	for (int32 i = 0; i < num; ++i) {
		p2[i].push_back(p1[i]);
	}
}


template<typename T1, typename T2>
void f_Array2D数据转Array2D数据(T1& 目标, T2& 源) {
	uint32 num = 源.size();
	目标.resize(num);

	auto* p1 = 源.data();
	auto* p2 = 目标.data();
	//#pragma loop(on_vector)
	for (int32 i = 0; i < num; ++i) {
		uint32 jNum = p1[i].size();
		p2[i].resize(jNum);
		auto* p1_1 = p1[i].data();
		auto* p2_1 = p2[i].data();

		for (int32 j = 0; j < jNum; ++j) {
			p2_1[j] = p1_1[j];
		}

		//p2[i].push_back(p1[i]);
	}
}



#define DEF_Array2D转Py数据(data, range, v, type)					\
	for (int32 y = (range).x; y < (range).y; ++y) {					\
		uint32 xNum = (v)[y].size();	\
		const auto* p1 = (v)[y].data();	\
		PyObject* e = PyList_New(xNum);	\
			\
		for (uint32 x = 0; x < xNum; ++x) {	\
			PyList_SetItem(e, x, Py_BuildValue((type), p1[x]));	\
		}	\
		PyList_SetItem((data), y, e);	\
	}	\



void f_F32_Arrat2D转Py数据(PyObject* data, const std::vector<std::vector<float32>>& v, uvec2 range, const char* type) {
	for (int32 y = (range).x; y < (range).y; ++y) {
		uint32 xNum = (v)[y].size();
		const auto* p1 = (v)[y].data();
		PyObject* e = PyList_New(xNum);

		//#pragma loop(on_vector)
		for (uint32 x = 0; x < xNum; ++x) {
			PyList_SetItem(e, x, Py_BuildValue(type, p1[x]));
		}
		PyList_SetItem(data, y, e);
	}

	//DEF_Array2D转Py数据( data, v, range, type );
}

void f_I32_Arrat2D转Py数据(PyObject* data, const std::vector<std::vector<int32>>& v, uvec2 range, const char* type) {
	for (int32 y = (range).x; y < (range).y; ++y) {
		uint32 xNum = (v)[y].size();
		const auto* p1 = (v)[y].data();
		PyObject* e = PyList_New(xNum);

		//#pragma loop(on_vector)
		for (uint32 x = 0; x < xNum; ++x) {
			PyList_SetItem(e, x, Py_BuildValue( (type), p1[x]));
		}
		PyList_SetItem( (data), y, e);
	}
}


template<typename T>
PyObject* f_Array2D数据转PY输出(T& v, const char* type) {
	uint32 yNum = v.size();
	const auto* p = v.data();

	PyObject* data = PyList_New(yNum);

//#pragma loop(on_vector)
#pragma omp parallel for
	for (int32 y = 0; y < yNum; ++y) {
		uint32 xNum = p[y].size();
		const auto& p2 = p[y].data();
		PyObject* e = PyList_New(xNum);

//#pragma loop(on_vector)
#pragma omp parallel for
		for (int32 x = 0; x < xNum; ++x) {
			PyList_SetItem(e, x, Py_BuildValue(type, p2[x]));
		}
		PyList_SetItem(data, y, e);
	}

	return data;
}





C_Array2D_F32插座::C_Array2D_F32插座(std::wstring name)
	:C_插座基类(name, E_值类型::e_插座Type_F32_Array2D)
{
	m_Value.resize(1);
}

C_Array2D_F32插座::~C_Array2D_F32插座()
{
}

void* C_Array2D_F32插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_插座Type_F32_Array2D) {
			return dynamic_cast<C_Array2D_F32插座*>(socket)->f_getData(线程ID);
		}
		else if (socket->m_Type == E_值类型::e_Type_F32_Array1D) {
			auto& data = *DEF_F32_Array1D插座数据(socket, 0);
			//f_Array1D数据转Array2D数据(m_Value, data);
		}
		else if (socket->m_Type == E_值类型::e_Type_I32_1D) {
			auto& data = *DEF_I32插座_1D数据(socket, 0);
			//f_Array1D数据转Array2D数据(m_Value, data);
		}
		else if (socket->m_Type == E_值类型::e_插座Type_I32_Array2D) {
			auto& data = *DEF_取2DArray_I32插座数据(socket, 线程ID);
			f_Array2D数据转Array2D数据(m_Value, data);
		}
	}
	return &m_Value;
}

void C_Array2D_F32插座::f_setData(void * data, int64 ID) {
	m_Value = *((std::vector<std::vector<float32>>*)data);
}

void* C_Array2D_F32插座::f_getData(const uvec4& id) {
	if (id.x < m_Value.size()) {
		return &m_Value[id.x];
	}
	return nullptr;
}

uvec4 C_Array2D_F32插座::f_get维度() {
	uint32 x = m_Value.size();
	if (x) {
		return { x, uint32(m_Value[0].size()), 0, 0 };
	}
	return { uint32(m_Value.size()), 0, 0, 0 };
}

PyObject * C_Array2D_F32插座::f_输出_bl数据(PyObject * bl_data) {
	const auto& v = *DEF_取2DArray_F32插座数据(this, 0);
	return f_F32_Array2D_输出_bl数据(v, bl_data);
}



PyObject* f_F32_Array2D_输出_bl数据(const std::vector<std::vector<float32>>& v, PyObject* bl_data)
{
	uint32 num = v.size();
	PyObject* data = PyList_New(num);

	if (num >= 64) {
		C_线程池 线程池(num);

		线程池.f_运行([&](std::thread& t, uvec2& r, uint32 i) {
			t = std::thread(f_F32_Arrat2D转Py数据, data, std::ref(v), r, "f");
			});
		线程池.f_等待();
	}
	else {
		f_F32_Arrat2D转Py数据(data, std::ref(v), {0, num}, "f");
	}
	

	return data;
}











void f_Array1D_int8转int32() {

}



C_插座I32_1D::C_插座I32_1D(std::wstring name) :C_插座基类(name, E_值类型::e_Type_I32_1D) {
	m_默认值 = (S_I32Array*)f_core_array_new(1, sizeof(int32));
	m_转换值 = (S_I32Array*)f_core_array_new(1, sizeof(int32));
	m_Value = m_默认值;
}

C_插座I32_1D::~C_插座I32_1D() {
	f_core_array_free((S_Array*)m_默认值);
	f_core_array_free((S_Array*)m_转换值);
}

void* C_插座I32_1D::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		switch (socket->m_Type){
			case E_值类型::e_Type_Array:
			case E_值类型::e_Type_I32_1D: {
				return (socket)->f_getData(0);
			}
			case E_值类型::e_Type_I8_1D: {
				const auto* data = DEF_I8插座_1D数据(socket);
				f_core_array_resize((S_Array*)m_转换值, data->count);
				for (uint32 i = 0; i < data->count; ++i) {
					m_转换值->ptr_userData[i] = data->ptr_userData[i];
				}
				break;
			}
			case E_值类型::e_Type_F32_1D: {
				const auto* data = DEF_F32插座_1D数据(socket);
				f_core_array_resize((S_Array*)m_转换值, data->count);
				for (uint32 i = 0; i < data->count; ++i) {
					m_转换值->ptr_userData[i] = data->ptr_userData[i];
				}
				break;
			}
			case E_值类型::e_Type_I32: {
				int32 data = DEF_I32插座数据(socket, 0);
				f_core_array_resize((S_Array*)m_转换值, 1);
				m_转换值->ptr_userData[0] = data;
				break;
			}
			case E_值类型::e_Type_UI32: {
				uint32 data = DEF_UI32插座数据(socket, 0);
				f_core_array_resize((S_Array*)m_转换值, 1);
				m_转换值->ptr_userData[0] = data;
				break;
			}
		}
		m_Value = m_转换值;
	}
	else if (!m_映射指针) {
		m_Value = m_默认值;
	}
	return m_Value;
}

void C_插座I32_1D::f_setData(void* data, uint8 线程ID) {
	if (data) {
		m_Value = ((S_I32Array*)data);
		m_映射指针 = true;
	}
	else {
		m_Value = m_默认值;
		m_映射指针 = false;
	}
}

std::string C_插座I32_1D::f_构建变量代码(int32 loc) {
	std::string code;
	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == e_Type_I32_1D || socket->m_Type == e_Type_Array) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + f_整数值转字符串(loc) + "new_val_to_I32s";

		code += DEF_S(S_I32Array*)" " + m_Code + " = ";
		switch (socket->m_Type) {
			case e_Type_I8:
			case e_Type_I32:
			case e_Type_UI32:
			case e_Type_F32: {
				code += DEF_S(f_array_I32fromF32_alloc)"(" + socket->f_getCode(loc) + ");\n";
				break;
			}
			case E_值类型::e_Type_UI32_1D: {
				code += DEF_S(f_array_UI32fromI32_alloc)"(" + socket->f_getCode(loc) + ");\n";
				break;
			}
			case E_值类型::e_Type_I8_1D: {
				code += DEF_S(f_array_I32fromI8_alloc)"(" + socket->f_getCode(loc) + ");\n";
				break;
			}
			case E_值类型::e_Type_F32_1D: {
				code += DEF_S(f_array_I32fromF32s_alloc)"(" + socket->f_getCode(loc) + ");\n";
				break;
			}
			default:
				break;
		}

		m_pNode->m_ReleaseCode += f_代码构建_销毁数组(m_Code);
	}
	else {
		C_插座基类::f_构建变量代码(loc);
		m_Code = "(&" + m_Code + ")";
	}

	return code;
}

bool C_插座I32_1D::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_I8:
		case E_值类型::e_Type_F32:
		case E_值类型::e_Type_I32:
		case E_值类型::e_Type_UI32:
		case E_值类型::e_Type_F32_1D:
		case E_值类型::e_Type_I8_1D:
		case E_值类型::e_Type_I32_1D:
		case E_值类型::e_Type_UI32_1D:
		case E_值类型::e_Type_Array:
			return true;
	}
	return false;
}

void C_插座I32_1D::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
	fread(m_默认值->ptr_userData, sizeof(int32), 1, f);
}

void C_插座I32_1D::f_写入(FILE* f) {
	C_插座基类::f_写入(f);
	fwrite(m_默认值->ptr_userData, sizeof(int32), 1, f);
}



PyObject* f_I32_Array1D_输出_bl数据(const std::vector<int32>& v) {
	uint32 num = v.size();

	PyObject* data = PyList_New(num);
	auto* p1 = v.data();

	for (uint32 i = 0; i < num; ++i) {
		PyList_SetItem(data, i, Py_BuildValue("i", v[i]));
	}

	return data;
}












C_插座I32_Array2D::C_插座I32_Array2D(std::wstring name)
	:C_插座基类(name, E_值类型::e_插座Type_I32_Array2D)
{
	m_Value.resize(1);
}

C_插座I32_Array2D::~C_插座I32_Array2D()
{
}

void* C_插座I32_Array2D::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_插座Type_I32_Array2D || socket->m_Type == E_值类型::e_Type_Array) {
			return dynamic_cast<C_插座I32_Array2D*>(socket)->f_getData(线程ID);
		}
		if (socket->m_Type == E_值类型::e_Type_面顶点索引) {
			return dynamic_cast<C_面顶点索引插座*>(socket)->f_getData(线程ID);
		}
		else if (socket->m_Type == E_值类型::e_插座Type_F32_Array2D) {
			auto& data = *DEF_F32_Array1D插座数据(socket, 0);
			//f_Array1D数据转Array2D数据(m_Value, data);
		}
		else if (socket->m_Type == E_值类型::e_插座Type_UI32_Array2D) {
			auto& data = *DEF_I32插座_1D数据(socket, 0);
			//f_Array1D数据转Array2D数据(m_Value, data);
		}
	}
	return &m_Value;
}

void C_插座I32_Array2D::f_setData(void * data, uint8 线程ID) {
	m_Value = *((std::vector<std::vector<int32>>*)data);
}












C_UI32插座_Array1D::C_UI32插座_Array1D(std::wstring name) :C_插座基类(name, E_值类型::e_Type_UI32_1D) {
	m_默认值 = (S_UI32Array*)f_core_array_new(1, sizeof(uint32));
	m_转换值 = (S_UI32Array*)f_core_array_new(1, sizeof(uint32));

	m_默认值->ptr_userData[0] = 0;
	m_转换值->ptr_userData[0] = 0;
	m_Value = m_默认值;
}

C_UI32插座_Array1D::~C_UI32插座_Array1D() {
	f_core_array_free((S_Array*)m_默认值);
	f_core_array_free((S_Array*)m_转换值);
	C_插座基类::~C_插座基类();
}

void* C_UI32插座_Array1D::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_UI32_1D || socket->m_Type == E_值类型::e_Type_Array) {
			return socket->f_getData(线程ID);
		}

		switch (socket->m_Type) {
			case E_值类型::e_Type_UI32_2D: {
				auto* data = DEF_UI32插座_2D数据(socket);
				if (data->count) {
					f_core_array_copy((S_Array*)m_转换值, (S_Array*)data->ptr_userData[0]);
				}
				else {
					f_core_array_resize((S_Array*)m_转换值, 0);
				}
				break;
			}
		}
		return m_转换值;
	}
	else if (!m_映射指针) {
		m_映射指针 = m_默认值;
	}
	return m_Value;
}

void C_UI32插座_Array1D::f_setData(void * data, uint8 线程ID) {
	if (data) {
		m_Value = ((S_UI32Array*)data);
		m_映射指针 = true;
	}
	else {
		m_Value = m_默认值;
		m_映射指针 = false;
	}
}

std::string C_UI32插座_Array1D::f_构建变量代码(int32 loc) {
	std::string code;
	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == e_Type_UI32_1D || socket->m_Type == e_Type_Array) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + f_整数值转字符串(loc) + "new_val_to_UI32s";

		code += DEF_S(S_UI32Array*)" " + m_Code + " = ";
		switch (socket->m_Type) {
			case e_Type_I8:
			case e_Type_I32:
			case e_Type_UI32:
			case e_Type_F32: {
				code += DEF_S(f_array_fromF32_alloc)"(" + socket->f_getCode(loc) + ");\n";
				break;
			}
			case E_值类型::e_Type_I32_1D: {
				code += DEF_S(f_array_UI32fromI32_alloc)"(" + socket->f_getCode(loc) + ");\n";
				break;
			}
			case E_值类型::e_Type_F32_1D: {
				code += DEF_S(f_array_UI32fromF32s_alloc)"(" + socket->f_getCode(loc) + ");\n";
				break;
			}
			case E_值类型::e_Type_UI32_2D: {
				code += DEF_S(f_array_UI32fromUI32s2D_alloc)"(" + socket->f_getCode(loc) + ");\n";
				break;
			}
			default:
				std::cout<<"error:"<< m_pNode->f_getName() << "/" << f_getName() << "转换没有实现类型" << std::endl;
				break;
		}

		m_pNode->m_ReleaseCode += f_代码构建_销毁数组(m_Code);
	}
	else {
		C_插座基类::f_构建变量代码(loc);
		m_Code = "(&" + m_Code + ")";
	}

	return code;
}

bool C_UI32插座_Array1D::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
	case E_值类型::e_Type_F32:
	case E_值类型::e_Type_I32:
	case E_值类型::e_Type_UI32:
	case E_值类型::e_Type_F32_1D:
	case E_值类型::e_Type_I32_1D:
	case E_值类型::e_Type_UI32_1D:
	case E_值类型::e_Type_Vec2_1D:
	case E_值类型::e_Type_Vec3_1D:
	case E_值类型::e_Type_Vec4_1D:
	case E_值类型::e_Type_iVec2_1D:
	case E_值类型::e_Type_iVec3_1D:
	case E_值类型::e_Type_uVec2_1D:
	case E_值类型::e_Type_uVec3_1D:

	case E_值类型::e_Type_UI32_2D:
		return true;
	}
	return false;
}

void C_UI32插座_Array1D::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
	fread(m_默认值->ptr_userData, sizeof(uint32), 1, f);
}

void C_UI32插座_Array1D::f_写入(FILE* f) {
	C_插座基类::f_写入(f);
	fwrite(m_默认值->ptr_userData, sizeof(uint32), 1, f);
}















C_UI32插座_Array3D::C_UI32插座_Array3D(std::wstring name) :C_插座基类(name, E_值类型::e_插座Type_UI32_Array3D) {

}

void* C_UI32插座_Array3D::f_getData() {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_插座Type_UI32_Array3D) {
			return dynamic_cast<C_UI32插座_Array3D*>(socket)->f_getData();
		}
		else if (socket->m_Type == E_值类型::e_Type_F32_Array1D) {
			//auto& data = *DEF_取F32_Array1D_数据(socket);
			//m_Value.clear();
			//m_Value.assign(data.begin(), data.end());
		}
		else if (socket->m_Type == E_值类型::e_Type_I32_1D) {
			//auto& data = *DEF_取1DVector_I32插座数据(socket);
			//m_Value.clear();
			//m_Value.assign(data.begin(), data.end());
		}
		else if (socket->m_Type == E_值类型::e_插座Type_引用数据) {
			/*auto* data = DEF_转换为引用数据(socket);

			switch (data->m_Type)
			{
			case E_值类型::e_Type_UI32_Array1D:
			case E_值类型::e_Type_I32_Array1D:
			case E_值类型::e_插座Type_UInt32:
			case E_值类型::e_插座Type_Int32:
				return data;
			default:
				break;
			}*/

		}
	}
	return &m_Value;
}

void C_UI32插座_Array3D::f_setData(void* data, int64 ID) {
	if (ID < 0) {
		//m_Value = *((vector<>))
	}
}

PyObject* C_UI32插座_Array3D::f_bl_传入数据(PyObject* bl_data) {
	std::string type;// = f_Py字符串转string(PyList_GetItem(bl_data, 0));
	if (type == "初始值") {
		m_Value.resize(1);
		m_Value[1].resize(1);
		m_Value[1][1].resize(1);

		float32 value = PyFloat_AsDouble(PyList_GetItem(bl_data, 1));
		for (auto& z : m_Value) for (auto& y : z) for (auto& x : y) x = value;
	}
	return nullptr;
}

PyObject* C_UI32插座_Array3D::f_输出_bl数据(PyObject* bl_data) {
	return nullptr;
}

void C_UI32插座_Array3D::f_删除元素(uint32 id) {
	auto& data = *DEF_取UI32_Array3D插座数据(this);
	if (id < data.size()) {
		data.erase(data.begin() + id);
	}
	else {
		std::cout << "错误输出施工中" << std::endl;
	}
}

void C_UI32插座_Array3D::f_释放数据() {

}





C_数组插座::C_数组插座(std::wstring name) :C_插座基类(name, E_值类型::e_Type_Array) {
	m_默认值 = f_core_array_new(1, sizeof(uint8));
	m_Value = m_默认值;
}

C_数组插座::~C_数组插座() {
	f_core_array_free(m_默认值);
}

void* C_数组插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(0);
	if (socket && f_代码构建_数组指针(socket)) {
		return socket->f_getData(0);
	}
	return m_Value;
}

void C_数组插座::f_setData(void* data, uint8 线程ID) {
	m_Value = (S_Array*)data;
}

std::string C_数组插座::f_构建变量代码(int32 loc) {
	std::string code;
	C_插座基类* socket = f_getLinkOutSocket(0);

	if (socket) {
		if (f_代码构建_数组指针(socket)) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_Array";

		switch (socket->m_Type) {
		/*case e_Type_I32:
		case e_Type_UI32:
		case e_Type_F32: {
			code += "S_F32Array* " + m_Code + " = f_f32_array_fromF32_alloc(" + socket->f_getCode() + ");\n";
			break;
		}*/
		default:
			return "";
		}

		m_pNode->m_ReleaseCode += f_代码构建_销毁数组(m_Code);
	}
	else {
		C_插座基类::f_构建变量代码(loc);
		m_Code = "(&" + m_Code + ")";
		return "";
	}

	
	return code;
}

bool C_数组插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_Bool_1D:
		case E_值类型::e_Type_I8_1D:
		case E_值类型::e_Type_I32_1D:
		case E_值类型::e_Type_UI32_1D:
		case E_值类型::e_Type_F32_1D:
		case E_值类型::e_Type_Vec4_1D:

		case E_值类型::e_Type_Vec3_1D:
		case E_值类型::e_Type_Vec3_2D:

		case E_值类型::e_Type_iVec2_1D:

		case E_值类型::e_Type_iVec3_1D:
		case E_值类型::e_Type_iVec3_2D:

		case E_值类型::e_Type_uVec2_1D:
		case E_值类型::e_Type_uVec3_1D:
		case E_值类型::e_Type_UI32_2D:
		case E_值类型::e_Type_多边形边1D:
		case E_值类型::e_Type_Object_1D:
		case E_值类型::e_Type_多边形元素1D:
		case E_值类型::e_Type_材质:
		case E_值类型::e_Type_材质1D:
		case E_值类型::e_Type_纹理:
		case E_值类型::e_Type_纹理1D:
		case E_值类型::e_Type_Array:
			return true;
	}
	return false;
}




C_2D数组插座::C_2D数组插座(std::wstring name) :C_插座基类(name, E_值类型::e_Type_Array1D) {
	m_默认 = (S_1DArray*)f_core_array_new(1, sizeof(S_1DArray));
	//m_Value = (S_1DArray*)f_core_array_new(0, sizeof(S_1DArray));
}

void* C_2D数组插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		switch (socket->m_Type) {
		case E_值类型::e_Type_Array1D:
			return socket->f_getData(0);
			
		case E_值类型::e_Type_Array:
			m_默认->ptr_userData[0] = nullptr;
			break;
		}

		m_Value = m_默认;
	}
	else {
		m_Value = m_默认;
	}
	return m_Value;
}

void C_2D数组插座::f_setData(void* data, uint8 线程ID) {
	m_Value = (S_1DArray*)data;
}

std::string C_2D数组插座::f_构建变量代码(int32 loc) {
	std::string code;
	C_插座基类* socket = f_getLinkOutSocket(loc);

	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Array1D) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_Array";

		code = "S_1DArray* " + m_Code + " = (S_1DArray*)f_core_array_new(1, sizeof(S_1DArray), false);\n";
		switch (socket->m_Type) {
		case E_值类型::e_Type_Array:
			break;
		}

		m_pNode->m_ReleaseCode += f_代码构建_销毁数组(m_Code);
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_2D数组插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
	case E_值类型::e_Type_Array:
	case E_值类型::e_Type_Array1D:
		return true;
	}
	return false;
}
